package com.filldream.fastboot.web.controller.app;

import com.filldream.fastboot.base.entity.Account;
import com.filldream.fastboot.base.request.app.account.ChangePhoneSTO;
import com.filldream.fastboot.base.request.app.account.ResetPwdSTO;
import com.filldream.fastboot.common.util.entity.wx.WxInitSTO;
import com.filldream.fastboot.base.request.app.account.ModifyAccountSTO;
import com.filldream.fastboot.base.request.app.account.PasswordLoginSTO;
import com.filldream.fastboot.base.response.app.AccountInfoDTO;
import com.filldream.fastboot.base.response.app.AccountManagerDTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.manager.AccountManager;
import com.filldream.fastboot.core.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author RickSun && iFillDream
 * @version 1.0
 * @data 2020-05-07 17:49
 * @description AccountController
 */
@Api(tags = "APP-用户相关")
@RestController
@RequestMapping("/app/user")
public class AccountController {

    @Resource
    private AccountService accountService;

    @Resource
    private AccountManager accountManager;

    @ApiOperation(value = "微信授权/登录")
    @PostMapping(value = {"/wxLogin"})
    public R<AccountManagerDTO> wxLogin(@RequestBody @Valid WxInitSTO loginSTO) {
        return accountService.wxLogin(loginSTO);
    }

    @ApiOperation(value = "密码登陆")
    @PostMapping(value = {"/login"})
    public R<AccountManagerDTO> login(@RequestBody @Valid PasswordLoginSTO passwordLoginSTO){
        return accountService.login(passwordLoginSTO);
    }

    @ApiOperation(value = "个人信息")
    @PostMapping("/getInfo")
    public R<AccountInfoDTO> getInfo() {
        Long userId = LoginHolder.getUserId();
        return R.autoSuccess(  accountManager.getById(userId) );
    }

    @ApiOperation(value = "修改信息")
    @PostMapping(value = {"/modify"})
    public B modify(@RequestBody ModifyAccountSTO modifyAccountSTO){
        Account account = TransferUtil.changeToObj(modifyAccountSTO, Account.class);
        account.setId( LoginHolder.getUserId() );
        return accountManager.modify( account ) ? B.success() : B.fail();
    }

    @ApiOperation(value = "退出",notes = "必需token")
    @PostMapping("/loginOut")
    public B loginOut(){
        return accountService.loginOut();
    }



    //---------手机短信相关----------

    @PostMapping("/reset")
    @ApiOperation(value = "找回密码")
    public B reset(@RequestBody @Valid ResetPwdSTO resetPwdSTO) {
        return accountService.reset(resetPwdSTO);
    }

    @PostMapping("/changePhone")
    @ApiOperation(value = "更换手机号",notes = "改完退不退出看自己吧,发送给新的手机号")
    public B changePhone(@RequestBody @Valid ChangePhoneSTO changePhoneSTO) {
        return accountService.changePhone(changePhoneSTO);
    }

}
